<?php
class EnterAction extends CommonAction {

    public function index()
    {
        $enter = M('Enter');
        $account = M('enter_account');
        $enterData = $enter->order('enter_date desc')->select();
        $allDate = array();

        foreach($enterData as $k=>&$v)
        {
            $allDate[] = $v['enter_date'];
        }

        if(!isset($_GET['date']))
        {
            $lastEnter = $enter->order('enter_date desc')
                               ->limit(1)
                               ->find();

        }
        else
        {
            $lastEnter = $enter->order('enter_date desc')
                                  ->where("enter_date='$_GET[date]'")
                                  ->limit(1)
                                  ->find();
        }

        $accountData = $account->where("stuff_date='$lastEnter[enter_date]'")->select();

        $lastEnter['enter_content'] = unserialize($lastEnter['enter_content']);
        $stuffs = array();
        $priceCount = 0;
        foreach($lastEnter['enter_content'] as $k=>$v)
        {
            $priceCount += $v['price_count'];

            foreach($v['stuff'] as $k2=>$v2)
            {
                $stuffs[] = $v2;
            }
        }

        foreach($stuffs as $k=>&$v)
        {
            foreach($stuffs as $k2=>&$v2)
            {
                if($v[0] == $v2[0] && $v[5] != $v2[5])
                {
                    $v[2] += $v2[2];
                    unset($stuffs[$k2]);
                }

                $temp = M('Stuff')->field(array('stuff_price', 'stuff_enter_price'))->where('id=' . $v[0])->find();
                if($temp['stuff_enter_price'] != 0.00) 
                    $v[4] = $temp['stuff_enter_price'];
            }
        }

        $this->assign(
            array(
                'allDate' => $allDate,
                'lastEnter' => $lastEnter,
                'stuffs' => $stuffs,
                'priceCount' => $priceCount,
                'accountData' => $accountData
            )
        );

        $this->display();
    }


    public function enterAccount()
    {
        $account = M('enter_account');
        $enter = M('enter');

        $count = sizeof($this->_post('stuff_name'));

        $stuffName = $this->_post('stuff_name');
        $stuffId = $this->_post('stuff_id');
        $stuffNumber = $this->_post('stuff_number');
        $stuffNum = $this->_post('stuff_num');
        $stuffUnit = $this->_post('stuff_unit');
        $stuffPrice = $this->_post('stuff_price');
        $stuffCount = $this->_post('stuff_count');
        $stuffDate = $this->_post('stuff_date');

        for($i=0; $i<$count; $i++)
        {
            $data['stuff_number'] = $stuffNumber[$i];
            $data['stuff_name'] = $stuffName[$i];
            $data['stuff_id'] = $stuffId[$i];
            $data['stuff_num'] = $stuffNum[$i];
            $data['stuff_unit'] = $stuffUnit[$i];
            $data['stuff_price'] = $stuffPrice[$i];
            $data['stuff_count'] = $stuffCount[$i];
            $data['stuff_date'] = $stuffDate[$i];

            $account->add($data);

            $otherData['id'] = $data['stuff_id'];
            $otherData['stuff_enter_price'] = $data['stuff_price'];
            M('Stuff')->save($otherData);
        }

        $enter->where('id='.$this->_post('enter_id'))->save(array('is_enter_stock'=>1));

        $this->success('已进入入库台帐，请查看', __URL__.'/index');
    }
}